package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsOuttask;
import org.jeecg.modules.job.rms.entity.RmsOuttaskApply;
import org.jeecg.modules.job.rms.service.IRmsOuttaskApplyService;
import org.jeecg.modules.job.rms.service.IRmsOuttaskService;
import org.jeecg.modules.job.ums.service.IUmsMemberRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 外包任务相关接口
 */
@RestController
@RequestMapping("/api/task")
@Api(tags="移动端外包任务管理模块")
@Slf4j
public class TaskController{

    @Autowired
    private IRmsOuttaskService taskService;
    @Autowired
    private IRmsOuttaskApplyService taskApplyService;
    @Autowired
    private IUmsMemberRoleService roleService;

    /**
     * 查询外包任务列表
     */
    @RequestMapping(value = "/getTaskList",method = RequestMethod.GET)
    @ApiOperation(value = "查询外包任务列表", notes = "查询外包任务列表")
    public Result<Object> getTaskList(RmsOuttask param,
                                      @RequestParam(name="page", defaultValue="1") Integer page,
                                      @RequestParam(name="limit", defaultValue="10") Integer limit
    ) {
        IPage<RmsOuttask> pageInfo= taskService.findPageBySpec(new Page<>(page,limit),param);
        return Result.OK(pageInfo);
    }


    /**
     * 查询我的外包任务列表
     */
    @RequestMapping(value = "/getMyTaskList",method = RequestMethod.GET)
    @ApiOperation(value = "查询我的外包任务列表", notes = "查询我的外包任务列表")
    public Result<Object> getMyTaskList(RmsOuttask param,
                                        @RequestParam(name="page", defaultValue="1") Integer page,
                                        @RequestParam(name="limit", defaultValue="10") Integer limit) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        param.setMemberId(user.getId());
        IPage<RmsOuttask> pageInfo= taskService.findPageBySpec(new Page<>(page,limit),param);
        return Result.OK(pageInfo);
    }

    /**
     * 查询外包任务详情
     */
    @RequestMapping(value = "/getTaskDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询外包任务详情", notes = "查询外包任务详情")
    public Result<Object> getTaskDetail(String id) {
        RmsOuttask result=taskService.getById(id);
        try{
            //查看是否已报名
            RmsOuttaskApply param=new RmsOuttaskApply();
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            param.setTaskId(id);
            RmsOuttaskApply ar=taskApplyService.getOne(new QueryWrapper<>(param));
            if (ar!=null){
                result.setIfApply(true);
            }else{
                result.setIfApply(false);
            }
        }catch (Exception e){
           System.err.println( e.getMessage());
        }
        return Result.OK(result);
    }

    /**
     * 添加/更新外包任务
     */
    @RequestMapping(value = "/addOrUpdateTask",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新外包任务", notes = "添加/更新外包任务")
    public Result<Object> addOrUpdateTask(@RequestBody RmsOuttask param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            param.setCompanyId(roleService.getCompanyId(user.getId()));
            taskService.addOrUpdate(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除外包任务
     */
    @RequestMapping(value = "/deleteTask",method = RequestMethod.POST)
    @ApiOperation(value = "删除外包任务务", notes = "删除外包任务")
    public Result<Object> deleteTask(@RequestBody RmsOuttask param) {
        try {
            taskService.removeById(param.getId());
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 外包任务报名
     */
    @RequestMapping(value = "/applyTask",method = RequestMethod.POST)
    @ApiOperation(value = "外包任务报名", notes = "外包任务报名")
    public Result<Object> applyTask(@RequestBody RmsOuttaskApply param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            taskApplyService.applyTask(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询报名信息
     */
    @RequestMapping(value = "/getApplyTaskDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询报名信息", notes = "查询报名信息，任务ID查询")
    public Result<Object> getApplyTaskDetail(@RequestBody RmsOuttaskApply param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            RmsOuttaskApply result=taskApplyService.getOne(new QueryWrapper<>(param));
            return Result.OK(result);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    /**
     * 外包任务
     */
    @RequestMapping(value = "/getApplyTaskList",method = RequestMethod.GET)
    @ApiOperation(value = "我的外包任务", notes = "我的外包任务")
    public Result<Object> getApplyTaskList(RmsOuttaskApply param,
                                           @RequestParam(name="page", defaultValue="1") Integer page,
                                           @RequestParam(name="limit", defaultValue="10") Integer limit) {
        if (param.getTaskId()==null){
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
        }
        IPage<RmsOuttaskApply> pageInfo= taskApplyService.findPageBySpec(new Page<>(page,limit),param);
        return Result.OK(pageInfo);
    }

}



